<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html lang="en">
<head>
    <base href="<%=basePath%>">
    <meta charset="utf-8"/>
    <title></title>
    <meta name="description" content="overview & stats"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <link href="static/css/bootstrap.min.css" rel="stylesheet"/>
    <link href="static/css/bootstrap-responsive.min.css" rel="stylesheet"/>
    <link rel="stylesheet" href="static/css/font-awesome.min.css"/>
    <!-- 下拉框 -->
    <link rel="stylesheet" href="static/css/chosen.css"/>
    <link rel="stylesheet" href="static/css/ace.min.css"/>
    <link rel="stylesheet" href="static/css/ace-responsive.min.css"/>
    <link rel="stylesheet" href="static/css/ace-skins.min.css"/>
    <script type="text/javascript" src="static/js/jquery-1.7.2.js"></script>
    <!--提示框-->
    <script type="text/javascript" src="static/js/jquery.tips.js"></script>

    <script type="text/javascript" src="static/js/select2-4.0.3/js/select2.full.js"></script>
    <link rel="stylesheet" href="static/js/select2-4.0.3/css/select2.min.css" media="all">

    <script type="text/javascript">
        $(top.hangge());
        $(document).ready(function () {
            // debugger
            if ($("#user_id").val() != "") {
                $("#loginname").attr("readonly", "readonly");
                $("#loginname").css("color", "gray");
            }
            var roleid = "${pd.ROLE_ID}"
            // console.log(roleid)

            if (roleid == "e148852b899e4714aa175e1306dbef32" || roleid == "8516c618faff4a5fbf33a15f7e33a911" || roleid == '${WWZY}') {//催收组长||催收员||委外专员
                if (roleid == "e148852b899e4714aa175e1306dbef32") {//催收组长
                    var url = "user/findAllManagers";
                    // var args={"orgId":org_id};
                    $.getJSON(url, function (data) {
                        // debugger
//                console.log("执行的是催收组长")
//                 console.log(data)
                        $("#parent_id option:not(:first)").remove();
                        for (var i = 0; i < data.length; i++) {
                            var city_id = data[i].USER_ID;
                            var city_name = data[i].NAME;
                            $("#parent_id").append("<option value='" + city_id + "'>" + city_name + "</option>");
                        }
                        <%--console.log("parent_id1="+"${pd.PARENT_ID}")--%>
                        <%--console.log("org_id1="+"${pd.ORGANIZATION_ID}")--%>
                        $("#parent_id").val("${pd.PARENT_ID}")
                        $("#org_id").val("${pd.ORGANIZATION_ID}")
                    });
                    //changgezhiwei();
                }
                if (roleid == "8516c618faff4a5fbf33a15f7e33a911") {//催收员
                    var url = "user/findGroupLeaderByOrgId";
                    var org_id = "${pd.ORGANIZATION_ID}"
                    var args = {"orgId": org_id};
//            console.log("org_id："+org_id)
//            console.log("org_id")
                    if ($("select[mark='zhiwei']").val() == "8516c618faff4a5fbf33a15f7e33a911") {
                        $.getJSON(url, args, function (data) {
//                    console.log("执行的是催收员初始化")
//                    console.log($("select[mark='zhiwei']").val())
                            console.log(data)
                            $("#parent_id option:not(:first)").remove();
                            for (var i = 0; i < data.length; i++) {
                                var city_id = data[i].USER_ID;
                                var city_name = data[i].NAME;
                                $("#parent_id").append("<option value='" + city_id + "'>" + city_name + "</option>");
                            }
                            <%--console.log("parent_id2="+"${pd.PARENT_ID}")--%>
                            <%--console.log("org_id2="+"${pd.ORGANIZATION_ID}")--%>
                            $("#parent_id").val("${pd.PARENT_ID}")
                            $("#org_id").val("${pd.ORGANIZATION_ID}")
                        });
                    }
                    //  changgezhiwei();
                }
                if (roleid == '${WWZY}') {//委外专员
                    var url = "user/findAllWWGSManagers";
                    $.getJSON(url, function (data) {
                        console.log("执行的是委外专员")
                        console.log(data)
                        $("#parent_id option:not(:first)").remove();
                        for (var i = 0; i < data.length; i++) {
                            var city_id = data[i].USER_ID;
                            var city_name = data[i].NAME;
                            $("#parent_id").append("<option value='" + city_id + "'>" + city_name + "</option>");
                        }
                        $("#org_id").attr("disabled", "disabled");
                        $("#parent_id").val("${pd.PARENT_ID}")

                    });
                }

            } else {
                console.log("页面初始化禁用")
                $("#org_id").attr("disabled", "disabled");
                $("#parent_id").attr("disabled", "disabled");
                //changgezhiwei();
            }
            initSelect();//初始化启用不器用

        });

        function initSelect() {
            var data = [{id: ' ', text: '请选择'}, {id: 0, text: '启用'}, {id: 1, text: '不启用'}];
            var select2Obj = $("#STATUS").select2({
                data: data
            });
            select2Obj.val("${pd.STATUS}").trigger("change");
            return select2Obj;
        }

        function changgezhiwei() {
            console.log($("select[mark='zhiwei']").val())
            var zhiweiStr = $("select[mark='zhiwei']").val()
            if (zhiweiStr == "e148852b899e4714aa175e1306dbef32") {
//            console.log("催收组长")
                $("#org_id").removeAttr("disabled");
                $("#parent_id").removeAttr("disabled");
                $("#org_id").val("-1");
                $("#parent_id").val("-1");
                $("#parent_id  option:not(:first)").remove();
                var url = "user/findAllManagers";
                // var args={"orgId":org_id};
                $.getJSON(url, function (data) {
//               console.log("执行的是催收组长")
//               console.log(data)
                    $("#parent_id option:not(:first)").remove();
                    for (var i = 0; i < data.length; i++) {
                        var city_id = data[i].USER_ID;
                        var city_name = data[i].NAME;
                        $("#parent_id").append("<option value='" + city_id + "'>" + city_name + "</option>");
                    }
                });


            } else if (zhiweiStr == "8516c618faff4a5fbf33a15f7e33a911") {
                //   console.log("催收员")
                $("#org_id").removeAttr("disabled");
                $("#parent_id").removeAttr("disabled");
                $("#org_id").val("-1");
                $("#parent_id").val("-1");
                $("#parent_id  option:not(:first)").remove();
                if ($("select[mark='zhiwei']").val() == "8516c618faff4a5fbf33a15f7e33a911") {
//               console.log("催收员111")
//               console.log($("select[mark='zhiwei']").val())
                    $("#org_id").change(function () {
                        //使#city只保留第一个option子节点
                        // $("#org_id option:not(:first)").remove();
                        var org_id = $(this).val();
//                   console.log("org_id:" + org_id)
                        if (org_id != "") {
                            var url = "user/findGroupLeaderByOrgId";
                            var args = {"orgId": org_id};
                            if ($("select[mark='zhiwei']").val() == "8516c618faff4a5fbf33a15f7e33a911") {
                                $.getJSON(url, args, function (data) {
//                               console.log("执行的是催收员")
//                               console.log($("select[mark='zhiwei']").val())
//                               console.log(data)
                                    $("#parent_id option:not(:first)").remove();
                                    for (var i = 0; i < data.length; i++) {
                                        var city_id = data[i].USER_ID;
                                        var city_name = data[i].NAME;
                                        $("#parent_id").append("<option value='" + city_id + "'>" + city_name + "</option>");
                                    }
                                });
                            }
                        }
                    });
                }
            } else if (zhiweiStr == '${WWZY}') {
                // debugger
                console.log("委外专员")
                // $("#org_id").removeAttr("disabled");
                $("#parent_id").removeAttr("disabled");
                // $("#org_id").val("-1");
                if (!$("#parent_id").val()) {
                    $("#parent_id").val("-1");
                }
                $("#parent_id  option:not(:first)").remove();
                if ($("select[mark='zhiwei']").val() == '${WWZY}') {
                    console.log("委外专员111")
                    console.log($("select[mark='zhiwei']").val())
                    var url = "user/findAllWWGSManagers";
                    if ($("select[mark='zhiwei']").val() == '${WWZY}') {
                        $.getJSON(url, function (data) {
                            console.log("执行的是委外专员")
                            console.log($("select[mark='zhiwei']").val())
                            console.log(data)
                            $("#parent_id option:not(:first)").remove();
                            for (var i = 0; i < data.length; i++) {
                                var city_id = data[i].USER_ID;
                                var city_name = data[i].NAME;
                                $("#parent_id").append("<option value='" + city_id + "'>" + city_name + "</option>");
                            }
                        });
                    }
                }
            } else {
//           console.log("其他")
                $("#org_id").attr("disabled", "disabled");
                ;
                $("#parent_id").attr("disabled", "disabled");
                $("#org_id").val("-1");
                $("#parent_id").val("-1");
            }
        }

        //保存
        function save() {
            if ($("#role_id").val() == "") {

                $("#role_id").tips({
                    side: 3,
                    msg: '选择角色',
                    bg: '#AE81FF',
                    time: 2
                });

                $("#role_id").focus();
                return false;
            }
            if ($("#loginname").val() == "" || $("#loginname").val() == "此用户名已存在!") {

                $("#loginname").tips({
                    side: 3,
                    msg: '输入用户名',
                    bg: '#AE81FF',
                    time: 2
                });

                $("#loginname").focus();
                $("#loginname").val('');
                $("#loginname").css("background-color", "white");
                return false;
            } else {
                $("#loginname").val(jQuery.trim($('#loginname').val()));
            }

            if ($("#NUMBER").val() == "") {

                $("#NUMBER").tips({
                    side: 3,
                    msg: '输入编号',
                    bg: '#AE81FF',
                    time: 3
                });
                $("#NUMBER").focus();
                return false;
            }

            if ($("#user_id").val() == "" && $("#password").val() == "") {

                $("#password").tips({
                    side: 3,
                    msg: '输入密码',
                    bg: '#AE81FF',
                    time: 2
                });

                $("#password").focus();
                return false;
            }
            if ($("#password").val() != $("#chkpwd").val()) {

                $("#chkpwd").tips({
                    side: 3,
                    msg: '两次密码不相同',
                    bg: '#AE81FF',
                    time: 3
                });

                $("#chkpwd").focus();
                return false;
            }
            if ($("#name").val() == "") {

                $("#name").tips({
                    side: 3,
                    msg: '输入姓名',
                    bg: '#AE81FF',
                    time: 3
                });
                $("#name").focus();
                return false;
            }

            if ($("#user_id").val() == "") {
                hasU();
            } else {
                $("#userForm").submit();
                $("#zhongxin").hide();
                $("#zhongxin2").show();
            }
        }

        function ismail(mail) {
            return (new RegExp(/^(?:[a-zA-Z0-9]+[_\-\+\.]?)*[a-zA-Z0-9]+@(?:([a-zA-Z0-9]+[_\-]?)*[a-zA-Z0-9]+\.)+([a-zA-Z]{2,})+$/).test(mail));
        }

        //判断用户名是否存在
        function hasU() {
            var USERNAME = $("#loginname").val();
            var url = "<%=basePath%>user/hasU.do?USERNAME=" + USERNAME + "&tm=" + new Date().getTime();
            $.get(url, function (data) {
                if (data == "error") {
                    $("#loginname").css("background-color", "#D16E6C");

                    setTimeout("$('#loginname').val('此用户名已存在!')", 500);

                } else {
                    $("#userForm").submit();
                    $("#zhongxin").hide();
                    $("#zhongxin2").show();
                }
            });
        }


    </script>
</head>
<body>
<br>
<form action="user/${msg }.do" name="userForm" id="userForm" method="post">
    <input type="hidden" name="USER_ID" id="user_id" value="${pd.USER_ID }"/>
    <input type="hidden" name="USERNAME" value="${pd.USERNAME}"/>
    <div id="zhongxin" style="margin-left: 15px;">
        <table>

            <tr>
                <td><input type="password" name="PASSWORD" id="password" maxlength="32" placeholder="输入密码" title="密码"/>
                </td>
            </tr>
            <tr>
                <td><input type="password" name="chkpwd" id="chkpwd" maxlength="32" placeholder="确认密码" title="确认密码"/>
                </td>
            </tr>
            <c:if test="${userupPassWord == '1'}">
                <tr>
                    <td><input type="number" name="PHONE" id="PHONE" value="${pd.PHONE }" maxlength="32"
                               placeholder="这里输入手机号" title="手机号"/></td>
                </tr>
                <tr>
                    <td><input type="text" name="machineNum" id="machineNum" value="${pd.machine_num }"
                               placeholder="这里输入分机号" maxlength="64" title="分机号"/></td>
                </tr>
            </c:if>
            <tr>
                <td style="text-align: center;">
                    <a class="btn btn-mini btn-primary" onclick="save();">保存</a>
                    <a class="btn btn-mini btn-danger" onclick="top.Dialog.close();">取消</a>
                </td>
            </tr>
        </table>
    </div>
    <div id="zhongxin2" class="center" style="display:none"><br/><br/><br/><br/><img
            src="static/images/jiazai.gif"/><br/><h4 class="lighter block green"></h4></div>
</form>
<!-- 引入 -->
<script type="text/javascript">window.jQuery || document.write("<script src='static/js/jquery-1.9.1.min.js'>\x3C/script>");</script>
<script src="static/js/bootstrap.min.js"></script>
<script src="static/js/ace-elements.min.js"></script>
<script src="static/js/ace.min.js"></script>
<script type="text/javascript" src="static/js/chosen.jquery.min.js"></script><!-- 下拉框 -->
</body>
</html>